<?php
namespace XtwsBack\Controller;
use Think\Controller;

class CategoryController extends CommonController {

    // 分类列表
    public function index(){
        $cate = M('category');
        $count = $cate->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,100);// 实例化分页类 传入总记录数和每页显示的记录数(25),查出一百条记录到前台，再由前台js处理成分页
        $show = $Page->show();// 分页显示输出
        $list = $cate->order('displayorder desc')->where('status <> -1')->limit($Page->firstRow.','.$Page->listRows)->select();
        $list = $this->list_to_tree($list); //转换为分类树
//        dump($list);
        $this -> assign('list',$list);
        $this -> assign('page',$show);
        //加载视图
        $this->display();
    }//end fun



    // 添加修改分类
    public function addCate(){
        $cate = M('category');
        $id  = intval(I('id'));
        if(IS_POST){
            $data = I('post.') ;
            if($id){//编辑
                $res = $cate -> save($data);
            }else{//添加
                $data['add_time'] = time();
                $res = $cate -> add($data);
            }// end if
            if($res){
                $this -> success('提交成功','http://pt.xiangtws.com/XtwsBack/Category/index');
            }else{
                $this -> error('提交失败');
            }// end if
        }else{
            //查询所有分类
            $cate_list = $cate->where('status <> -1')->field('id,pid,title')->select();
            $cate_list = $this->list_to_tree($cate_list); //转换为分类树
            if($id){
                $cateInfo = $cate->where(array('id'=>$id))->find();
            }else{
                $cateInfo = 1;
            }
            $this -> assign('cateInfo',$cateInfo);
            $this -> assign('cate_list',$cate_list);
            $this -> display();
        }// end if

    }// end fun


    // 删除分类
    public function del_cate(){
        $id  = intval(I('id'));
        if($id){
            $cate = M('category');
            $data['status'] = -1;
            $data['id'] = $id;
            $res = $cate->save($data);
//            $res = $cate->delete($id);
            if($res){
                $this->success('删除成功');
            }else{
                $this->error('出错啦，请检查');
            }
        }else{
            $this->error('请先选择数据');
        }

    }//end fun

    /**
     * 将返回的数据集转换成树
     * @param  array   $list  数据集
     * @param  string  $pk    主键
     * @param  string  $pid   父节点名称
     * @param  string  $child 子节点名称
     * @param  integer $root  根节点ID
     * @return array          转换后的树
     */
    public function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0) {
        $tree = array();
        // 创建Tree
        if (is_array($list)) {
            // 创建基于主键的数组引用
            $refer = array();
            foreach ($list as $key => $data) {
                //将数据的id转换为它的键
                $refer[$data[$pk]] = &$list[$key];
            }
            foreach ($list as $key => $data) {
                // 判断是否存在parent
                $parentId = $data[$pid];
                //查询顶级分类
                if ($root == $parentId) {
                    $tree[$data[$pk]] = &$list[$key];
                } else {
                    if (isset($refer[$parentId])) {
                        $parent = &$refer[$parentId];
                        $parent[$child][] = &$list[$key];
                        //dump($list[$key]);die;
                    }
                }
            }
        }
        return $tree;
    }//end fun



}